package com.takeeasy.platform.control;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.HashMap;
import java.util.Map;

import javax.servlet.ServletException;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.mysql.jdbc.StringUtils;
import com.takeeasy.platform.cache.CurrentThreadPool;
import com.takeeasy.platform.dataobject.ResponseDataObject;
import com.takeeasy.platform.delegate.BizDelegate;
import com.takeeasy.platform.util.JSONUtil;
import com.takeeasy.platform.util.URLUtil;

public class CommonController extends BaseController {
	private static final Logger logger = LoggerFactory.getLogger(CommonController.class);

	@Override
	public void excute() {
		this.parseRequest();
		ResponseDataObject resp = BizDelegate.delegate(getRequestDataObject());
		response(resp);
	}

	private void response(ResponseDataObject resp) {
		Map<String, String> actionInfo = CurrentThreadPool.getActionInfoConfig();
		switch (actionInfo.get(URLUtil.SUBMIT_TYPE)) {
		case URLUtil.SUBMIT_TYPE_AJAX:
			ajax(resp);
			break;
		case URLUtil.SUBMIT_TYPE_FORM:
			form(resp);
			break;
		case URLUtil.SUBMIT_TYPE_FORWARD:
			forward(resp);
			break;
		case URLUtil.SUBMIT_TYPE_REDIRECT:
			redirect(resp);
			break;
		default:
			break;
		}

	}

	private void redirect(ResponseDataObject resp) {
		logger.debug("页面跳转使用重定向方式！");
		try {
			getHttpResponse().sendRedirect(resp.getRedirectPage());
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

	private void forward(ResponseDataObject resp) {
		logger.debug("页面跳转使用转发方式！");
		try {
			this.getHttpRequest().getRequestDispatcher(resp.getForwardPage()).forward(getHttpRequest(), getHttpResponse());
		} catch (ServletException | IOException e) {
			e.printStackTrace();
		}
	}

	private Map<String, Object> getResponseDataMap(ResponseDataObject resp) {
		Map<String, Object> respMap = new HashMap<>();
		if (resp == null) {
			logger.debug("为获取有效的返回对象！");
			return respMap;
		}
		respMap.put("page", resp.getPage());
		respMap.put("datas", resp.getResponseMap());
		respMap.put("error", resp.getErrorString());
		respMap.put("customerJson", resp.getResponseJsonString());
		return respMap;
	}

	private void form(ResponseDataObject resp) {
		logger.debug("页面提交为Form方式，页面将重定向！");
		getHttpResponse().setCharacterEncoding("UTF-8");
		this.getHttpRequest().setAttribute("returnValue", JSONUtil.getJSONString(getResponseDataMap(resp)));
		try {
			String page = "";
			if (resp != null) {
				page = resp.getForwardPage();
			} else {
				return;
			}
			if (page == null) {
				page = "error/error404.jsp";
			}
			this.getHttpRequest().getRequestDispatcher(page).forward(getHttpRequest(), getHttpResponse());
		} catch (ServletException | IOException e) {
			e.printStackTrace();
		}
	}

	private void ajax(ResponseDataObject resp) {
		logger.debug("页面提交为Ajax方式，将不返回页面！");
		String returnJsonString = "";
		String userJson = resp.getOriginalUserJson();
		if (!StringUtils.isNullOrEmpty(userJson)) {
			returnJsonString = userJson;
		} else {
			Map<String, Object> respMap = getResponseDataMap(resp);
			returnJsonString = JSONUtil.getJSONString(respMap);
		}
		getHttpResponse().setCharacterEncoding(resp.getEncode());
		getHttpResponse().setContentType(resp.getContentType());
		try {
			PrintWriter writer = getHttpResponse().getWriter();
			logger.debug("返回的数据是：" + returnJsonString);
			writer.write(returnJsonString);
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

}
